package com.yinkgh.mybatis.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import com.yinkgh.mybatis.common.service.impl.BaseServiceImpl;
import com.yinkgh.mybatis.dao.PerformanceAssessmentMapper;
import com.yinkgh.mybatis.dao.WorkTargetMapper;
import com.yinkgh.mybatis.model.PerformanceAssessment;
import com.yinkgh.mybatis.model.WorkTarget;
import com.yinkgh.mybatis.service.PerformanceAssessmentService;
import com.yinkgh.mybatis.view.PerformanceAssessmentView;

/**
 * 绩效考核service
 * @author yinlong
 *
 */
@Transactional(readOnly=true)
@Service
public class PerformanceAssessmentServiceImpl extends BaseServiceImpl<PerformanceAssessment> implements PerformanceAssessmentService {

	@Autowired
	private PerformanceAssessmentMapper performanceAssessmentMapper;
	
	@Autowired
	private WorkTargetMapper workTargetMapper;
	
	public PerformanceAssessmentMapper getDao() {
		return performanceAssessmentMapper;
	}
	
	@Override
	public PerformanceAssessmentView getPerformance(Integer id) {
		PerformanceAssessmentView performanceAssessmentView = performanceAssessmentMapper.getPerformance(id);
		List<WorkTarget> workTargets  = workTargetMapper.getWorkTargetsByPerformanceId(id);
		performanceAssessmentView.setWorkTargets(workTargets);
		return performanceAssessmentView;
	}
	
	@Override
	public Workbook getPerformanceAssessmentExcel(Integer id, String fileType) throws Exception {
		
		PerformanceAssessmentView performanceAssessmentView = performanceAssessmentMapper.getPerformance(id);
		List<WorkTarget> workTargets  = workTargetMapper.getWorkTargetsByPerformanceId(id);
		performanceAssessmentView.setWorkTargets(workTargets);
		//获取文件路径
		File excel = ResourceUtils.getFile("classpath:PerformanceAssessment.xlsx");
		//将文件读取到流中保证文件不修改
		InputStream inFile = new FileInputStream(excel);   
		//读取本地excel模版
		Workbook  wb = new XSSFWorkbook(inFile);
		inFile.close();
		//设置sheet名称
		wb.setSheetName(0, "月度绩效考核评估表");
		Sheet sheet = wb.getSheetAt(0);
		//向模版中指定的位置写值

		//修改部门职位等个人信息
		Row row4 = sheet.getRow(4);
		Cell row4_Cell1 = row4.getCell(1);
		
		row4_Cell1.setCellValue("部门：" + performanceAssessmentView.getDepartment() + 
				"        岗位：" + performanceAssessmentView.getPost() +
				"        被考核人：" + performanceAssessmentView.getBeAssessedPerson() + 
				"        考核人：" + performanceAssessmentView.getAssessPerson());
		
		//==================一、行为规范=====================
		Row row8  = sheet.getRow(8);
		Cell row8_Cell8 = row8.getCell(8);
		row8_Cell8.setCellValue(performanceAssessmentView.getObserveDisciplineSelfAssessment());
		Cell row8_Cell9 = row8.getCell(9);
		row8_Cell9.setCellValue(performanceAssessmentView.getObserveDisciplineSuperiorAssessment());
		
		Row row9  = sheet.getRow(9);
		Cell row9_Cell8 = row9.getCell(8);
		row9_Cell8.setCellValue(performanceAssessmentView.getStriveSelfAssessment());
		Cell row9_Cell9 = row9.getCell(9);
		row9_Cell9.setCellValue(performanceAssessmentView.getStriveSuperiorAssessment());
		
		Row row10  = sheet.getRow(10);
		Cell row10_Cell8 = row10.getCell(8);
		row10_Cell8.setCellValue(performanceAssessmentView.getCommunicateSelfAssessment());
		Cell row10_Cell9 = row10.getCell(9);
		row10_Cell9.setCellValue(performanceAssessmentView.getCommunicateSuperiorAssessment());
		
		//==================二、专业素质=====================
		Row row12  = sheet.getRow(12);
		Cell row12_Cell8 = row12.getCell(8);
		row12_Cell8.setCellValue(performanceAssessmentView.getWorkSkillSelfAssessment());
		Cell row12_Cell9 = row12.getCell(9);
		row12_Cell9.setCellValue(performanceAssessmentView.getWorkSkillSuperiorAssessment());
		
		Row row13  = sheet.getRow(13);
		Cell row13_Cell8 = row13.getCell(8);
		row13_Cell8.setCellValue(performanceAssessmentView.getInnovateSelfAssessment());
		Cell row13_Cell9 = row13.getCell(9);
		row13_Cell9.setCellValue(performanceAssessmentView.getInnovateSuperiorAssessment());
		
		Row row14  = sheet.getRow(14);
		Cell row14_Cell8 = row14.getCell(8);
		row14_Cell8.setCellValue(performanceAssessmentView.getKnowledgeSelfAssessment());
		Cell row14_Cell9 = row14.getCell(9);
		row14_Cell9.setCellValue(performanceAssessmentView.getKnowledgeSuperiorAssessment());
		
		Row row15  = sheet.getRow(15);
		Cell row15_Cell8 = row15.getCell(8);
		row15_Cell8.setCellValue(performanceAssessmentView.getWorkflowStandardSelfAssessment());
		Cell row15_Cell9 = row15.getCell(9);
		row15_Cell9.setCellValue(performanceAssessmentView.getWorkflowStandardSuperiorAssessment());
		
		Row row16  = sheet.getRow(16);
		Cell row16_Cell8 = row16.getCell(8);
		row16_Cell8.setCellValue(performanceAssessmentView.getWorkflowResultSelfAssessment());
		Cell row16_Cell9 = row16.getCell(9);
		row16_Cell9.setCellValue(performanceAssessmentView.getWorkflowResultSuperiorAssessment());
		
		//==================三、专业素质=====================
		if (workTargets != null && workTargets.size() > 0) {
			Integer workTargetsSize = workTargets.size();
			int rowNum = 18;
			for (int i = 0; i < workTargetsSize; i++) {
				Row row  = sheet.getRow(rowNum);
				Cell cell2 = row.getCell(2);
				Cell cell8 = row.getCell(8);
				Cell cell9 = row.getCell(9);
				
				cell2.setCellValue(workTargets.get(i).getTargetDescription());
				cell8.setCellValue(workTargets.get(i).getSelfAssessment());
				cell9.setCellValue(workTargets.get(i).getSuperiorAssessment());
				
				rowNum += 2;
				if (i >= 10) {
					break;
				}
			}
		}
		Integer rowOffset = 6;
		//==================四、突出业绩=====================
		Row row33  = sheet.getRow(33 + rowOffset);
		Cell row33_Cell8 = row33.getCell(8);
		row33_Cell8.setCellValue(performanceAssessmentView.getExtrudePerformanceSelfAssessment());
		Cell row33_Cell9 = row33.getCell(9);
		row33_Cell9.setCellValue(performanceAssessmentView.getExtrudePerformanceSuperiorAssessment());
		
		//==================五、工作失误=====================
		Row row34  = sheet.getRow(34 + rowOffset);
		Cell row34_Cell8 = row34.getCell(8);
		row34_Cell8.setCellValue(performanceAssessmentView.getWorkMistakeSelfAssessment());
		Cell row34_Cell9 = row34.getCell(9);
		row34_Cell9.setCellValue(performanceAssessmentView.getWorkMistakeSuperiorAssessment());
		
		//==================六、评分合计=====================
		Row row35 = sheet.getRow(35 + rowOffset);
		Cell row35_Cell8 = row35.getCell(8);
		row35_Cell8.setCellValue(performanceAssessmentView.getSelfAssessmentTotal());
		Cell row35_Cell9 = row35.getCell(9);
		row35_Cell9.setCellValue(performanceAssessmentView.getSuperiorAssessmentTotal());
		
		//==================七、加分说明=====================
		Row row36 = sheet.getRow(36 + rowOffset);
		Cell row36_Cell3 = row36.getCell(3);
		row36_Cell3.setCellValue(performanceAssessmentView.getAwardedMarksExplain());
		
		//==================八、扣分说明=====================
		Row row37 = sheet.getRow(37 + rowOffset);
		Cell row37_Cell3 = row37.getCell(3);
		row37_Cell3.setCellValue(performanceAssessmentView.getDeductMarksExplain());
		
		//==================九、本月主要工作总结=====================
		Row row38 = sheet.getRow(38 + rowOffset);
		Cell row38_Cell3 = row38.getCell(3);
		row38_Cell3.setCellValue(performanceAssessmentView.getCurrentMonthWorkSummary());
		
		//==================十、下月主要工作计划=====================
		Row row41 = sheet.getRow(41 + rowOffset);
		Cell row41_Cell3 = row41.getCell(3);
		row41_Cell3.setCellValue(performanceAssessmentView.getNextMonthWorkSummary());
		
		
		return wb;
	}

	
	
	
	
	
}
